﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YJKRGeometry;
using SectionSignAndFilled.StairsRoomSection.DataDefine;
using SectionSignAndFilled.StairsRoomSection.ElementPacket;

namespace SectionSignAndFilled.StairsRoomSection.Kit.CutLine
{
	internal class StairsLandingCutLineGetter : CutLineGetter
	{
		public override bool GetCutLines(SectionViewData ViewInfoRay, ElementObject elemObj)
		{
			List<Curve> list = new List<Curve>();
			SectionViewData sectionViewData;
			base.TransformViewData(elemObj, ViewInfoRay, out sectionViewData);
			Line secLine = sectionViewData.m_SecLine;
			if (!CommonFun.SureElementInPlanLine(elemObj.SelfElem.UseElement, secLine, sectionViewData.m_viewDir))
			{
				return true;
			}
			base.GetCutLines(ViewInfoRay, elemObj);
			list.AddRange(elemObj.m_lstCutLine);
			this.FindStructLayer(ref list, ViewInfoRay);
			elemObj.m_lstCutLine.Clear();
			elemObj.m_lstCutLine.AddRange(list);
			return true;
		}

		private void FindStructLayer(ref List<Curve> curves, SectionViewData viewSectionInfo)
		{
			List<List<Curve>> list = CutLineGetter.DivStairsCurves(curves, viewSectionInfo);
			curves = new List<Curve>();
			if (list == null || list.Count == 0)
			{
				return;
			}
			double val = 0.0;
			int index = 0;
			for (int i = 0; i < list.Count; i++)
			{
				XYZ xyz = XYZ.Zero;
				foreach (Curve curve in list[i])
				{
					xyz += curve.GetEndPoint(0);
					xyz += curve.GetEndPoint(1);
				}
				XYZ xyz2 = xyz / (double)(list[i].Count / 2);
				double z = xyz2.Z;
				if (i == 0)
				{
					val = z;
					index = i;
				}
				else if (Geometry.LessThan(z, val))
				{
					val = z;
					index = i;
				}
			}
			curves.Clear();
			curves.AddRange(list[index]);
		}
	}
}
